package cn.xeblog.plugin.tools.read.page;

import cn.xeblog.plugin.enums.Command;
import cn.xeblog.plugin.tools.read.ui.ReadmeDialog;

import javax.swing.*;
import java.awt.*;

/**
 * 开始页
 *
 * @author LYF
 * @date 2022-07-18
 */
public class StartPage implements IPage {

  /**
   * 开始面板
   */
  private JPanel startPanel;

  @Override
  public void show() {
    // 初始化开始页并展示
    if (startPanel == null) {
      initUI();
    }

    cn.xeblog.plugin.tools.read.page.UIManager.showPage(startPanel, 200, 200);
  }

  @Override
  public void initUI() {
    // 开始面板
    startPanel = new JPanel();
    startPanel.setBounds(10, 10, 120, 200);

    // 标题
    startPanel.add(getTitle());

    // 功能按钮
    Box vBox = Box.createVerticalBox();
    vBox.add(Box.createVerticalStrut(20));
    vBox.add(getStartButton());
    vBox.add(getSettingButton());
    vBox.add(getReadmeButton());
    vBox.add(getExitButton());
    startPanel.add(vBox);
  }

  /**
   * 获取标题组件
   *
   * @return 标题组件
   */
  private JLabel getTitle() {
    JLabel title = new JLabel("阅读!");
    title.setFont(new Font("", Font.BOLD, 14));
    return title;
  }

  /**
   * 获取按钮
   *
   * @return 开始阅读按钮
   */
  private JButton getStartButton() {
    JButton button = new JButton("开始阅读");
    button.addActionListener(e -> cn.xeblog.plugin.tools.read.page.UIManager.bookshelfPage.show());
    return button;
  }

  /**
   * 获取打开设置按钮
   *
   * @return 打开设置按钮
   */
  private JButton getSettingButton() {
    JButton button = new JButton("打开设置");
    button.addActionListener(e -> UIManager.settingPage.show());
    return button;
  }

  /**
   * 获取退出阅读按钮
   *
   * @return 退出阅读按钮
   */
  private JButton getExitButton() {
    JButton exitButton = new JButton("退出阅读");
    exitButton.addActionListener(e -> Command.OVER.exec());
    return exitButton;
  }

  /**
   * 获取使用说明按钮
   *
   * @return 使用说明按钮
   */
  private JButton getReadmeButton() {
    JButton exitButton = new JButton("使用说明");
    exitButton.addActionListener(e -> {
      new ReadmeDialog();
    });
    return exitButton;
  }
}
